import { IPublicModelPluginContext } from '@alilc/lowcode-types';
import { Button } from '@alifd/next';
import {
  saveSchema,
} from '../../services/mockService';

// 保存功能示例,预览
const PreviewSamplePlugin = (ctx: IPublicModelPluginContext) => {
  return {
    async init() {
      const { skeleton, config } = ctx;
      const doPreview = () => {
        console.log('预览');
        let scenarioName = config.get('scenarioName');
        const params = new URLSearchParams(window.location.search);
        const scenarioName2 = params.get('scenarioName');
        if (scenarioName2) {
          scenarioName = scenarioName2;
        }
        saveSchema(scenarioName);
        setTimeout(() => {
          // debugger
          let queryString = location.search;
          const params = new URLSearchParams(queryString);
          const scenarioName = params.get('scenarioName');
          if (scenarioName) {
            window.open(`./preview.html${scenarioName}`);
            return;
          }
          const search = location.search ? `${location.search}&scenarioName=${scenarioName}` : `?scenarioName=${scenarioName || 'general'}`;
          window.open(`./preview.html${search}`);
        }, 500);
      };
      skeleton.add({
        name: 'previewSample',
        area: 'topArea',
        type: 'Widget',
        props: {
          align: 'right',
        },
        content: (
          <Button type="primary" onClick={() => doPreview()}>
            预览
          </Button>
        ),
      });
    },
  };
}
PreviewSamplePlugin.pluginName = 'PreviewSamplePlugin';
PreviewSamplePlugin.meta = {
  dependencies: ['EditorInitPlugin'],
};
export default PreviewSamplePlugin;